package com.example.win.android_banduyujia.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.win.android_banduyujia.entity.CurrEntity;
import com.example.win.android_banduyujia.entity.MyProjectEntity;
import com.example.win.android_banduyujia.entity.OrderlistEntity;
import com.example.win.android_banduyujia.entity.TodayEntity;
import com.example.win.android_banduyujia.lib.datetime.DateTime;

import static com.example.win.android_banduyujia.database.BaseDatabaseHelper.PROJECT_TIME_WEEK;

/**
 * Created by Win on 2015/8/24.
 */
public abstract class BaseDatabaseHelper implements IDBHelper {
    public static final String  LOG_TAG = BaseDatabaseHelper.class.getSimpleName();

    private  DBHelper  mDBHelper;

    private static final int INT_TRUE = 1;
    private static final int INT_FALSE = 0;

    protected static final String ID = "_index";
    protected static final String ORDER_ID="_id";
    protected static final String ORDER_PROJECT_NAME="order_project_name";  // 课程名字
    protected static final String ORDER_PROJECT_TYPES="order_project_types"; // 课程类型
    protected static final String ORDER_TYPES_URL="order_types_url"; //类型url
    protected static final String ORDER_MONTH="order_month"; // 性别
    protected static final String ORDER_WEEK="order_week"; // 出生日期
    protected static final String ORDER_TIME="order_time"; // 上传图片
    protected static final String ORDER_TIME_URL="order_time_url"; // 授课范围
    protected static final String ORDER_PRICE="order_price"; // 签署场馆
    protected static final String ORDER_PAY_STATUS="order_pay_status"; // 实名认证
    protected static final String ORDER_PAY_URL="order_pay_url"; // 认证证书
    protected static final String ORDER_PAY_TIME="order_pay_time"; // 荣誉证

    public static final String SQL_ORDER_LIST_TABLE = "CREATE TABLE"
                     + "%1$s" + "("
                     + ID + "integer primary key autoincrement,"
                     + ORDER_ID + "integer,"
                     + ORDER_PROJECT_NAME + "text,"
                     + ORDER_PROJECT_TYPES + "text,"
                     + ORDER_TYPES_URL  + "text,"
                     + ORDER_MONTH + "text,"
                     + ORDER_WEEK + "text,"
                     + ORDER_TIME + "text,"
                     + ORDER_TIME_URL + "text,"
                     + ORDER_PRICE + "text,"
                     + ORDER_PAY_STATUS + "text,"
                     + ORDER_PAY_URL + "text,"
                     + ORDER_PAY_TIME + "text);";

    protected static final String __ID="_id";
    protected static final String PROJECT_ID="project_id";
    protected static final String PROJECT_NAME="project_name"; // 课程名字
    protected static final String PROJECT_TYPES="project_types"; // 类型
    protected static final String PROJECT_TYPES_URL="project_types_url"; // 类型Url
    protected static final String PROJECT_TIME_MONTH="project_time_month"; // 月份
    protected static final String PROJECT_TIME_WEEK="project_time_week"; // 星期
    protected static final String PROJECT_TIME="project_time"; //授课时间
    protected static final String PROJECT_TIME_URL="project_time_url"; // 授课时间Url
    protected static final String PROJECT_ADDRESS_URL="project_address_url"; // 上课地址
    protected static final String PROJECT_STAY_URL="project_stay_url"; // 待上课
    protected static final String PROJECT_COMPLETE_URL="project_complete_url"; // 已完成
    protected static final String PROJECT_DROP_URL="project_drop_url"; // 已退课

    public static final String SQL_CREATE_PROJECT="CREATE TABLE"
                      + "%1$s" + "("
                      + __ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
                      + PROJECT_ID + "text not null,"
                      + PROJECT_NAME + "text,"
                      + PROJECT_TYPES + "text,"
                      + PROJECT_TYPES_URL + "text,"
                      + PROJECT_TIME_MONTH + "integer,"
                      + PROJECT_TIME_WEEK + "integer,"
                      + PROJECT_TIME + "integer,"
                      + PROJECT_TIME_URL + "text,"
                      + PROJECT_ADDRESS_URL + "text,"
                      + PROJECT_STAY_URL + "text,"
                      + PROJECT_COMPLETE_URL + "text,"
                      + PROJECT_DROP_URL + "text);";


    private String  mTableName;

    protected  BaseDatabaseHelper(String  tableName){
        mTableName = tableName;
        try{
           mDBHelper = DBHelper.getInstance();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }
    }

    @Override
    public String getTableName() {
        return mTableName;
    }

    @Override
    public abstract void onCreate(SQLiteDatabase db);

    @Override
    public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion);

    protected MyProjectEntity getProjectInfo(Cursor cursor) {
        MyProjectEntity entity = new MyProjectEntity();
        int  index = cursor.getColumnIndex(PROJECT_ID);
        if(index !=-1)
            entity.setId(cursor.getInt(index));
        index = cursor.getColumnIndex(PROJECT_NAME);
        if(index !=-1)
            entity.setProjectName(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TYPES);
        if(index !=-1)
            entity.setProjectTypes(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TYPES_URL);
        if(index !=-1)
            entity.setTypesUrl(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME_MONTH);
        if(index !=-1)
            entity.setCreateMoth(new DateTime(Long.valueOf(cursor.getLong(index))));
        index = cursor.getColumnIndex(PROJECT_TIME_WEEK);
        if(index !=-1)
            entity.setCreateWeek(new DateTime(Long.valueOf(cursor.getLong(index))));
        index = cursor.getColumnIndex(PROJECT_TIME);
        if(index !=-1)
            entity.setProTime(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME_URL);
        if(index !=-1)
            entity.setTime(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_ADDRESS_URL);
        if(index !=-1)
            entity.setAddressUrl(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_STAY_URL);
        if(index !=-1)
            entity.setNoproject(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_COMPLETE_URL);
        if(index !=-1)
            entity.setComplete(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_DROP_URL);
        if(index !=-1)
            entity.setRefund(cursor.getString(index));
        return entity;
    }

    protected OrderlistEntity getOrderList(Cursor cursor) {
        OrderlistEntity  entity = new OrderlistEntity();
        int index = cursor.getColumnIndex(ORDER_ID);
        if(index !=-1)
            entity.setId(cursor.getInt(index));
        index = cursor.getColumnIndex(ORDER_PROJECT_NAME);
        if(index !=-1)
            entity.setProjectName(cursor.getString(index));
        index = cursor.getColumnIndex(ORDER_PROJECT_TYPES);
        if(index !=-1)
            entity.setProjectTypes(cursor.getString(index));
        index = cursor.getColumnIndex(ORDER_TYPES_URL);
        if(index !=-1)
            entity.setTypesUrl(cursor.getString(index));
        index = cursor.getColumnIndex(ORDER_MONTH);
        if(index !=-1)
            entity.setMonth(new DateTime(Long.valueOf(cursor.getLong(index))));
        index = cursor.getColumnIndex(ORDER_WEEK);
        if(index !=-1)
            entity.setWeek(new DateTime(Long.valueOf(cursor.getLong(index))));
        index = cursor.getColumnIndex(ORDER_PRICE);
        if(index !=-1)
            entity.setPrice(cursor.getString(index));
        index = cursor.getColumnIndex(ORDER_PAY_STATUS);
        if(index !=-1)
            entity.setPayStatus(cursor.getString(index));
        index = cursor.getColumnIndex(ORDER_PAY_URL);
        if(index !=-1)
            entity.setPayUrl(cursor.getString(index));
        index = cursor.getColumnIndex(ORDER_PAY_TIME);
        if(index !=-1)
            entity.setPayTime(cursor.getString(index));
        return  entity;
    }

    public CurrEntity getCurrentInfo(Cursor cursor) {
        CurrEntity  entity = new CurrEntity();
        int index = cursor.getColumnIndex(PROJECT_ID);
        if(index !=-1)
            entity.setId(cursor.getInt(index));
       index = cursor.getColumnIndex(PROJECT_NAME);
        if(index !=-1)
            entity.setProname(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TYPES);
        if(index !=-1)
            entity.setClassesTypes(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TYPES_URL);
        if(index !=-1)
            entity.setTypesUr(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME);
        if(index !=-1)
            entity.setProtime(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME_URL);
        if(index !=-1)
            entity.setTimeUrl(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME_MONTH);
        if(index !=-1)
            entity.setMonth(new DateTime(Long.valueOf(cursor.getInt(index))));
        index = cursor.getColumnIndex(PROJECT_TIME_WEEK);
        if(index !=-1)
            entity.setWeek(new DateTime(Long.valueOf(cursor.getInt(index))));
        index = cursor.getColumnIndex(PROJECT_ADDRESS_URL);
        if(index !=-1)
            entity.setProUrl(cursor.getString(index));

        return entity;
    }

    public TodayEntity getTodayInfo(Cursor cursor) {
        TodayEntity  entity = new TodayEntity();
        int index = cursor.getColumnIndex(PROJECT_ID);
        if(index !=-1)
            entity.setId(cursor.getInt(index));
        index = cursor.getColumnIndex(PROJECT_NAME);
        if(index !=-1)
            entity.setProname(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TYPES);
        if(index !=-1)
            entity.setClassesTypes(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TYPES_URL);
        if(index !=-1)
            entity.setTypesUr(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME);
        if(index !=-1)
            entity.setTime(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME_URL);
        if(index !=-1)
            entity.setTimeUrl(cursor.getString(index));
        index = cursor.getColumnIndex(PROJECT_TIME_MONTH);
        if(index !=-1)
            entity.setMonth(new DateTime(Long.valueOf(cursor.getInt(index))));
        index = cursor.getColumnIndex(PROJECT_TIME_WEEK);
        if(index !=-1)
            entity.setWeek(new DateTime(Long.valueOf(cursor.getInt(index))));
        index = cursor.getColumnIndex(PROJECT_ADDRESS_URL);
        if(index !=-1)
            entity.setProUrl(cursor.getString(index));

        return entity;
    }
}
